import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtCharts

Rectangle {
    Layout.fillWidth: true
    Layout.fillHeight: true

    ColumnLayout {
        anchors.fill: parent

        ChartView {
            title: "BarSeries 信号示例"
            titleFont.bold: true
            titleFont.pointSize: 14

            Layout.fillWidth: true
            Layout.fillHeight: true
            antialiasing: true

            BarCategoryAxis {
                id: axisX
                categories: ["1月", "2月", "3月", "4月"]
            }

            ValueAxis {
                id: axisY
                min: 0
                max: 20
            }

            BarSeries {
                id: barSeries
                axisX: axisX
                axisY: axisY
                barWidth: 0.6

                // 添加初始数据集
                BarSet {
                    id: barSet1
                    label: "数据集1"
                    values: [5, 10, 15, 12]
                    color: "#FF9612"
                }

                BarSet {
                    id: barSet2
                    label: "数据集2"
                    values: [8, 12, 10, 14]
                    color: "#1296FF"
                }

                // 连接所有信号
                onBarsetsAdded: function(barsets) {
                    logArea.append("[onBarsetsAdded]: 添加了 " + barsets.length + " 个数据集")
                }

                onBarsetsRemoved: function(barsets) {
                    logArea.append("[onBarsetsRemoved]: 移除了 " + barsets.length + " 个数据集")
                }

                onClicked: function(index, barset) {
                    logArea.append("[onClicked]: index=" + index + ", barset=" + barset.label)
                }

                onDoubleClicked: function(index, barset) {
                    logArea.append("[doubleClicked]: index=" + index + ", barset=" + barset.label)
                }

                onPressed: function(index, barset) {
                    logArea.append("[onPressed]: index=" + index + ", barset=" + barset.label)
                }

                onReleased: function(index, barset) {
                    logArea.append("[onReleased]: index=" + index + ", barset=" + barset.label)
                }
            }
        }

        // 控制按钮区域
        Row {
            id: buttonRow
            Layout.minimumHeight: 30
            Layout.fillWidth: true
            spacing: 10

            Button {
                text: "添加数据集"
                onClicked: {
                    // 创建新数据集并添加到系列中
                    var newBarSet = barSeries.append("数据集" + (barSeries.count + 1), [Math.random() * 10 + 5, Math.random() * 10 + 5, Math.random() * 10 + 5, Math.random() * 10 + 5])
                    newBarSet.color = Qt.rgba(Math.random(), Math.random(), Math.random(), 1)
                }
            }

            Button {
                text: "移除最后一个数据集"
                enabled: barSeries.count > 0
                onClicked: {
                    // 移除最后一个数据集
                    barSeries.remove(barSeries.at(barSeries.count - 1))
                }
            }

            Button {
                text: "清空日志"
                onClicked: {
                    logArea.text = "日志已清空\n"
                }
            }
        }

        // 用于显示信号触发信息的区域
        ScrollView {
            id: scrollView
            Layout.minimumHeight: 100
            Layout.maximumHeight: 100
            Layout.fillWidth: true
            Layout.margins: 10

            ScrollBar.vertical: ScrollBar {
                anchors.top: scrollView.top
                anchors.bottom: scrollView.bottom
                anchors.right: scrollView.right

                // 保持滚动到底端
                position: {
                    if (scrollView.height > 0 && scrollView.contentHeight > scrollView.height) {
                        return (scrollView.contentHeight - scrollView.height) / scrollView.contentHeight;
                    }
                    return 0;
                }
            }

            TextArea {
                id: logArea
                readOnly: true
                text: "信号日志将显示在这里...\n"
            }
        }
    }
}
